/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Product;

import JavaClass.ActionAll;
import Main.ExpoManagerMain;
import check.check;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Mai Lan
 */
public class ProductAction {
    public static void loadPro(JTable tbpro){
        Vector procp=new Vector();
        Vector profeil=new Vector();
         procp.clear();
            procp.add("ID");
            procp.add("ID Ex");
            procp.add("NameProduct");
            procp.add("DateCreate");
            procp.add("Status");
         profeil.clear();
            profeil.add("ID");
            profeil.add("IDExhibitor");
            profeil.add("NameProduct");
            profeil.add("DateCreate");
            profeil.add("Status");
         ActionAll.LoadInfor(procp, tbpro, profeil, "Product");
    }
     public static  void insertPro(JTable tbProduct,JComboBox cobIDex,JTextField txtNamePro,JTextField txtdaycreate,JTextArea  teaProduct){
       
           
                try {
                    boolean checkready = check.checkReadlyData("Product", "NameProduct", txtNamePro, ExpoManagerMain.conn);
                    if (checkready == false) {
                        return;
                    }
                    String sqlInPro = "Insert into Product (IDExhibitor,NameProduct,DateCreate,Status) values(?,?,?,?)";
                    PreparedStatement psInPro = ExpoManagerMain.conn.prepareStatement(sqlInPro);
                    psInPro.setString(1, (String) cobIDex.getSelectedItem());
                    psInPro.setString(2, txtNamePro.getText().trim());
                    psInPro.setString(3, txtdaycreate.getText().trim());
                    psInPro.setString(4, teaProduct.getText().trim());
                    psInPro.executeUpdate();
                    ProductAction.loadPro(tbProduct);
                    cobIDex.removeAllItems();
                    check.id(cobIDex, "Exhibitor");

                } catch (SQLException ex) {
                    Logger.getLogger(ProductAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            
       

    }
     public static  void UpPro(JTable tbProduct,JComboBox cobIDex,JTextField txtNamePro,JTextField txtdaycreate,JTextArea  teaProduct) throws SQLException {
        if(tbProduct.isCellSelected(tbProduct.getSelectedRow(), tbProduct.getSelectedColumn())==false){
                JOptionPane.showMessageDialog(null, "Please choice information want to edit!!!");
                return;
        }
        int n=tbProduct.getSelectedRow();
        String Id=(String) tbProduct.getValueAt(n, 0);

      
        
            try {
                String sqlUpPro="Update Product set IDExhibitor='"+(String)cobIDex.getSelectedItem()+"'," +
                  "NameProduct='"+txtNamePro.getText().trim()+"',DateCreate='"+txtdaycreate.getText().trim()+"',Status='"+teaProduct.getText().trim()+"' where ID="+Id;
                Statement st=ExpoManagerMain.conn.createStatement();
                st.executeUpdate(sqlUpPro);
                 ProductAction.loadPro(tbProduct);
                 cobIDex.removeAllItems();

                 check.id(cobIDex,"Exhibitor");
              
            } catch (SQLException ex) {
                Logger.getLogger(ProductAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
   public  static void Del(JTable tbProduct)throws SQLException{
            ActionAll.deleteInfor(tbProduct, "Product", "ID");
            ProductAction.loadPro(tbProduct);
    }
}

